package com.aos.controller;

import com.aos.entity.Permission;
import com.aos.permission.RequiredPermission;
import com.aos.service.permission.PermissionService;
import com.aos.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PermissionController
 * @Description:页面、菜单资源控制层
 * @Author wumingwang
 * @Date 2018/9/9 12:44
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(value = "Permission Controller", tags = {"资源菜单接口"})
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 分页查询菜单
     * @param code
     * @param type
     * @param parentName
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询菜单", notes = "分页查询菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "权限码",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "权限类型[页面元素:1;菜单:2]",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "parentName", value = "父菜单名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applicationId", value = "应用ID",  paramType = "query", dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int" ,example = "1"),
            @ApiImplicitParam(name = "limit", value = "页面大小", required = true, paramType = "query", dataType = "int",example = "10"),
    })
    @RequiredPermission
    @GetMapping("/permission/v1/queryByPage")
    public Object queryByPage(String code,String type,String parentName,String name,Long applicationId,int page, int limit){
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("type",type);
        map.put("parentName",parentName);
        map.put("name",name);
        map.put("applicationId",applicationId);
        map.put("pageNum",page);
        map.put("pageSize",limit);
        return permissionService.queryByPage(map);
    }

    /**
     * 新增菜单
     * @param permission
     * @return
     */
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父菜单ID",  dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "applicationId", value = "应用ID", required = true, dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "code", value = "权限码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "permissionType", value = "权限类型[页面元素:1;菜单:2]",required = true, dataType = "String"),
            @ApiImplicitParam(name = "url", value = "权限类型为页面元素:服务器接口;权限类型为菜单:前端URL", dataType = "String"),
            @ApiImplicitParam(name = "icon", value = "前端显示icon标识", dataType = "String"),
            @ApiImplicitParam(name = "sequence", value = "菜单显示顺序", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "菜单、按钮名称",required = true, dataType = "String"),
    })
    @RequiredPermission
    @PostMapping("/permission/v1/add")
    public Object addPermission(@Validated @RequestBody Permission permission){
        return permissionService.addPermission(permission);
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @DeleteMapping("/permission/v1/delete")
    public Object deletePermission(Long id){
        if(id == null){
            return Result.error("菜单ID不能为空！");
        }
        return permissionService.deleteById(id);
    }

    /**
     * 编辑菜单
     * @param permission
     * @return
     */
    @ApiOperation(value = "编辑菜单", notes = "编辑菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID",  required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "parentId", value = "父菜单ID",  dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "applicationId", value = "应用ID", required = true, dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "code", value = "权限码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "permissionType", value = "权限类型[页面元素:1;菜单:2]",required = true, dataType = "String"),
            @ApiImplicitParam(name = "url", value = "权限类型为页面元素:服务器接口;权限类型为菜单:前端URL", dataType = "String"),
            @ApiImplicitParam(name = "icon", value = "前端显示icon标识", dataType = "String"),
            @ApiImplicitParam(name = "sequence", value = "菜单显示顺序", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "菜单、按钮名称",required = true, dataType = "String"),
    })
    @RequiredPermission
    @PutMapping("/permission/v1/update")
    public Object updatePermission(@Validated @RequestBody Permission permission){
        if(permission.getId() == null){
            return Result.error("用户ID不能为空！");
        }
        return permissionService.updatePermission(permission);
    }

    /***
     * 查询菜单详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询菜单详情", notes = "查询菜单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @GetMapping("/permission/v1/queryDetails")
    public Object queryDetails(Long id){
        if(id == null){
            return Result.error("菜单ID不能为空！");
        }
        return permissionService.queryDetails(id);
    }

    /**
     * 根据应用ID查询菜单、按钮列表
     * @param applicationId
     * @return
     */
    @ApiOperation(value = "查询菜单、按钮树形列表", notes = "查询某个应用下所有菜单、按钮,返回树形结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicationId", value = "应用ID",  required = true, dataType = "Long",example = "0"),
    })
    @GetMapping("/permission/v1/queryTreeList")
    public Object queryList(Long applicationId){
        if(applicationId == null){
            return Result.error("应用ID不能为空！");
        }
        return permissionService.queryListByAppId(applicationId);
    }

    /**
     * 根据应用ID查询菜单（不包括按钮）列表
     * @param applicationId
     * @return
     */
    @ApiOperation(value = "查询菜单列表", notes = "查询某个应用下所有菜单（不包括按钮）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applicationId", value = "应用ID",  required = true, dataType = "Long",example = "0"),
    })
    @GetMapping("/permission/v1/queryMenuList")
    public Object queryMenuList(Long applicationId){
        if(applicationId == null){
            return Result.error("应用ID不能为空！");
        }
        return permissionService.queryMenuListByAppId(applicationId);
    }

}
